package view;


import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import controller.DrawEventListener.modeOptions;
import controller.DrawEventListener;
import model.ArrayQueue;
import model.ArrayStack;
import model.QueuePredict;
import model.StackPredict;

public class DrawWindow extends JPanel implements MouseListener{
	
	private static final long serialVersionUID = 1L;
	private StackPredict<String> stack;
	private QueuePredict<String> queue;
	private ArrayStack<String> arrayStack;
	private ArrayQueue<String> arrayQueue;
	private String status = "";
	private DrawEventListener drawListener;
	
	public DrawWindow(){
		drawListener = new DrawEventListener();
		addMouseListener(this);
	}
	
	public void updateStatus(String s){
		status = s;
		drawListener.updateStatus(s);
	}
	
	public void giveStack(StackPredict<String> s){
		stack = s;
		drawListener.updateStack(s);
	}
	public void giveArrayStack(ArrayStack<String> aS){
		arrayStack = aS;
		drawListener.updateArrayStack(aS);
	}
	
	public void giveQueue(QueuePredict<String> q) {
		queue = q;
		drawListener.updateQueue(q);
	}
	
	public void giveArrayQueue(ArrayQueue<String> aQ) {
		arrayQueue = aQ;
		drawListener.updateArrayQueue(aQ);
		
	}
	
	public void setPredictionMode(modeOptions b){
		drawListener.setPreidctionMode(b);
	}
	public void setInput(String s){
		drawListener.setInput(s);
	}
	
	public void paint(Graphics g ) {
		super.paint(g);

		if(stack != null && status.equals("STACK")){
			stack.paint(g);
		}
		
		if(queue != null && status.equals("QUEUE")){
			queue.paint(g);
		}
		
		if(arrayStack !=null && status.equals("ARRAYSTACK")){
			arrayStack.paint(g);
		}
		if(arrayQueue !=null && status.equals("ARRAYQUEUE")){
			arrayQueue.paint(g);
		}
	}
	
	
	public void clearWindow(){
		stack = new StackPredict<String>();
		queue = new QueuePredict<String>();
		arrayStack = new ArrayStack<String>();
		arrayQueue = new ArrayQueue<String>();
		drawListener.updateElements();
		repaint();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		drawListener.mouseClicked(e);
		if(drawListener.shouldPaint()){	
			repaint();
			if(drawListener.wasNotRemoved()){
				JOptionPane.showMessageDialog(this,"Your Prediction was wrong, Please try again!");	
				drawListener.setNotRemoved(false);
			}
			
			if(drawListener.getWasAdded()){
				JOptionPane.showMessageDialog(this,"Your Prediction was Correct!");
				drawListener.setWasAdded(false);
			}
			
			if(drawListener.getWasNotAdded()){
				JOptionPane.showMessageDialog(this,"Your Prediction was wrong, Try again!");	
			}
			if(drawListener.wasRemoved()){
				if( status.equals("STACK")){
					JOptionPane.showMessageDialog(this,"Correct! The element '" + drawListener.getRemoved() + "' was removed from the end of the stack ");
				}
				if( status.equals("QUEUE")){
					JOptionPane.showMessageDialog(this," Correct! The element '" + drawListener.getRemoved() + "' was removed from the end of the queue ");
				}
				if(status.equals("ARRAYSTACK")){
					JOptionPane.showMessageDialog(this,"Correct! The element '" + drawListener.getRemoved() + "' was removed from the end of the stack ");
				}
				if(status.equals("ARRAYQUEUE")){
					JOptionPane.showMessageDialog(this,"Correct! The element '" + drawListener.getRemoved() + "' was removed from the end of the queue ");			
				}
				drawListener.setRemoved(false);
			}
			drawListener.setPaint(false);
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	
}
